﻿'' GoogleCode - http://code.google.com/p/outlook-4-you/
'' GNU GPL v3 Licencing

Imports Microsoft.Office.Interop.Outlook
Imports System.Diagnostics

Public Class RequestAddIn '' This plugin produces build requests to a standard format
#Region "Declarations"
#Region "UI declarations"
    Private WithEvents cb As Office.CommandBar
    Private WithEvents cbbRequest As Office.CommandBarButton
    Private WithEvents cbbLeave As Office.CommandBarButton
    Private WithEvents cbbBuild As Office.CommandBarButton
    Private Const cb_name As String = "Request"
#End Region
#Region "Inspector UI"
    Private WithEvents cbt As Office.CommandBar
    Private WithEvents tbbCheck As Office.CommandBarButton

#End Region
#Region "Variable"
    Private WithEvents objApt As Outlook.AppointmentItem
    Private WithEvents objMail As Outlook.MailItem
    Private WithEvents objInspector As Outlook.Inspectors
    Private WithEvents CalItems, InboxItems, BuildItems As Outlook.Items
    Private olCal, olMail, olBuilds, olTasks As Outlook.Folder
    Private cLeave, pLeave, fLeave As Double
#End Region
#End Region

#Region "VSTO required"
    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        '' Shared Users Account
        'Dim Recip As Outlook.Recipient = Me.Application.GetNamespace("MAPI").CreateRecipient("Builder")
        'Recip.Resolve()
        'olMail = Me.Application.GetNamespace("MAPI").GetSharedDefaultFolder(Recip, Outlook.OlDefaultFolders.olFolderInbox)
        'olTasks = Me.Application.GetNamespace("MAPI").GetSharedDefaultFolder(Recip, OlDefaultFolders.olFolderTasks)
        'olCal = Me.Application.GetNamespace("MAPI").GetSharedDefaultFolder(Recip, Outlook.OlDefaultFolders.olFolderCalendar)

        olCal = Me.Application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar)
        olMail = Me.Application.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderInbox)
        olTasks = Me.Application.GetNamespace("MAPI").GetDefaultFolder(OlDefaultFolders.olFolderTasks)
        olBuilds = olMail.Parent.folders("Builds")
        objInspector = Application.Inspectors
        Add_Menubar()
    End Sub
    Private Sub ThisAddIn_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown

    End Sub
#End Region

#Region "UI"
    Private Sub Add_Menubar()
        '' Setup Commandbar
        Try
            cb = Application.CommandBars(cb_name)
        Catch ex As MissingMemberException
            cb = Me.Application.ActiveExplorer.CommandBars.Add(cb_name, Office.MsoBarPosition.msoBarRight, False, True)
        End Try
        cb.Visible = True

        '' Setup CommandBarButtons
        cbbRequest = cb.Controls.Add(Office.MsoControlType.msoControlButton, , , 1, True)
        With cbbRequest
            .Style = Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption
            .Caption = "Build Request"
            .FaceId = 2950  '3197 file instack, 3975 linked comps, 0627 barcode, 2173 wand, 2950 smiley
            .Tag = "request"
            .Visible = True
        End With
        AddHandler cbbRequest.Click, AddressOf Me.cbbRequest_Click
    End Sub    '' Creates button on commandbar
#End Region

#Region "Events"
    Private Sub cbbRequest_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
        If frmRequest.InstanceExists = False Then
            Dim req As New frmRequest("")
            req.Show()
        End If
    End Sub
#End Region

#Region "Outlook tasks"
    Public Shared Function Send_email(ByVal too As String, ByVal subj As String, ByVal body As String) As Boolean
        Dim request As Outlook.MailItem = Globals.RequestAddIn.Application.CreateItem(Outlook.OlItemType.olMailItem)
        Try
            With request
                .Subject = subj
                .Body = body
                .To = too
                .Importance = OlImportance.olImportanceHigh
                .Send()
            End With
            Return True
        Catch ex As SystemException
            MsgBox("Could not send email, Sorry")
            Return False
        End Try
    End Function
#End Region

#Region "Request Specific code"
    Private Sub Build_Handler()
        Try
            AddHandler Me.Application.NewMailEx, AddressOf Mail_Sorting
        Catch ex As System.Exception
            MsgBox("Unable to AddHandler to Inbox Items in MailItem_Handler" & vbCr & ex.Message.ToString, MsgBoxStyle.Information, "Appointment Monitor")
        End Try
    End Sub
    Private Sub Mail_Sorting()
        Dim ID As String = ""
        InboxItems = olMail.Items
        Dim items As Outlook.Items = InboxItems.Restrict("[UnRead] = true")
        For Each item In items
            If (TypeOf item Is Outlook.MailItem) Then
                objMail = TryCast(item, Outlook.MailItem)
                If objMail IsNot Nothing Then

                    If objMail.Subject.ToString.Contains("Find Build Request") = True Then  '' Build Search
                        Dim build As String = ""
                        Dim GUID As String = objMail.Body.ToString
                        GUID = GUID.Replace(vbCrLf, "")
                        If Search4Build(objMail.Body.ToString, build) = True Then
                            Send_email(objMail.SenderEmailAddress, "Found Build Request", build)
                        End If
                        objMail.Delete()    ' No longer needed so get rid

                    ElseIf objMail.Subject.ToString.Contains("Found Build Request") Then        '' Build Search Result
                        Dim req As New frmRequest(objMail.Body.ToString)
                        objMail.Delete()    ' No longer needed so get rid

                    End If
                End If
            End If
        Next

    End Sub
    Private Function Search4Build(ByVal GUID As String, ByRef build As String) As Boolean
        BuildItems = olTasks.Items
        For Each item In BuildItems
            If (TypeOf item Is Outlook.TaskItem) Then
                Dim objTask = TryCast(item, Outlook.TaskItem)
                If objTask IsNot Nothing Then

                    If objTask.EntryID.ToString = GUID Then
                        If objTask.Body.ToString.Contains(GUID) = True Then
                            build &= objTask.Body.ToString  '' FTTB Use reverse email
                        End If
                    End If
                End If
            End If
        Next
    End Function
#End Region

#Region "Miscellaneous"
    Private Sub WeekStarts(ByRef Start As Date, ByRef dEnd As Date)
        Select Case DateAndTime.Now.DayOfWeek
            Case DayOfWeek.Monday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(6)
            Case DayOfWeek.Tuesday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 1, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(5)
            Case DayOfWeek.Wednesday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 2, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(4)
            Case DayOfWeek.Thursday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 3, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(3)
            Case DayOfWeek.Friday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 4, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(2)
            Case DayOfWeek.Saturday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 5, 0, 0, 0)
                dEnd = DateAndTime.Now.AddDays(1)
            Case DayOfWeek.Sunday
                Start = New Date(DateAndTime.Now.Year, DateAndTime.Now.Month, DateAndTime.Now.Day - 6, 0, 0, 0)
                dEnd = DateAndTime.Now
        End Select
    End Sub
#End Region
End Class
